﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bootstrap;
using Dogshow.Win.DataAccess;
using Dogshow.Win.Models;
using Dogshow.Win.Views;
using Ninject;

namespace Dogshow.Win.UserControls
{
    public partial class IzborPsa : UserControl
    {
        
        public IzborPsa()
        {
            InitializeComponent();
            buttonEdit1.KeyDown += (s, e) =>
            {
                if (e.KeyData == Keys.Enter)
                {
                    OnUnetBrojRodovnika();
                }
            };
            buttonEdit1.ButtonClick += (s, e) => OnPretrazi();
        }

        


        private Model.Pas _pas;

        [Bindable(true)]
        public Model.Pas Pas
        {
            get { return _pas; }
            set
            {
                _pas = value;
                if (_pas != null)
                {
                    bindingSource1.DataSource = _pas;
                }
                else
                {
                    bindingSource1.DataSource = typeof (Model.Pas);
                }
            }
        }

        public void OnUnetBrojRodovnika()
        {
            var ds = GetDataService();
            try
            {
                var pas = ds.VratiPsaPoBrojuRodovnika(buttonEdit1.Text);
                Pas = ModelMapper.Map(pas);
            }
            catch (NotFoundException)
            {
                Pas = null;
            }
        }

        private void OnPretrazi()
        {
            using (var dlg = new PretragaPasa())
            {
                var res = dlg.ShowDialog(this);
                if (res == DialogResult.OK)
                {
                    Pas = dlg.IzabranPas;
                }
            }
        }

        private static IDataService GetDataService()
        {
            var kernel = (IKernel)Bootstrapper.Container;
            return kernel.Get<IDataService>();
        }
    }
}
